package com.wn55.hrms.web.controller;

import com.github.pagehelper.PageInfo;
import com.wn55.hrms.entity.Employee;
import com.wn55.hrms.entity.Salary;
import com.wn55.hrms.service.SalaryService;
import com.wn55.hrms.service.vo.EmployeeVo;
import com.wn55.hrms.util.ResultResponse;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("salary")
public class SalaryController {
    @Autowired
    private SalaryService salaryService;


    @GetMapping("/list")
    @ApiOperation(value = "分页查询薪水的列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "currentPage", value = "查询的当前页", required = false, defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageSize", value = "每页显示多少条", required = false, defaultValue = "10"),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "name", value = "管理员的用户名", required = false)
    })
    public ResultResponse<PageInfo<Salary>> list(
            @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            String name) {
        ResultResponse<PageInfo<Salary>> result;
        try {
            PageInfo<Salary> pageInfo = salaryService.listByCondition(currentPage, pageSize, name);
            result = new ResultResponse<>(200, "查询薪水成功!", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "查询薪水失败!");
        }
        return result;

    }

    @PostMapping("/add")
    @ApiOperation(value = "添加薪水记录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "currentPage", value = "查询的当前页", required = false, defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageSize", value = "每页显示多少条", required = false, defaultValue = "10"),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "name", value = "管理员的用户名", required = false)
    })
    public ResultResponse<Void> add(@RequestBody Map<String,Object> map) {
        ResultResponse<Void> result;
        try {
            Integer eId = (Integer) map.get("eId");
            String salaryMonth = (String) map.get("salaryMonth");
            salaryService.insertSalary(eId, salaryMonth);
            result = new ResultResponse<>(200, "添加薪水成功!");
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "添加薪水失败!");
        }
        return result;

    }


    @DeleteMapping("/del/{id}")
    public ResultResponse<Void> del(@PathVariable("id") Integer id){
        ResultResponse<Void> result = null;

        try {
            salaryService.removeByKey(id);
            result = new ResultResponse<>(200,"删除薪水记录成功!");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除薪水记录失败!");
        }
        return result ;
    }
}
